package org.sgx.gwteditors.testgallery.gallery;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.sgx.gwteditors.client.impl1.EditorFramework1;
import org.sgx.gwteditors.client.impl1.complex.PropertyHaverTableEditor2;
import org.sgx.gwteditors.client.impl1.complex.PropertyHaverTableEditor2.EditorClickHandler;
import org.sgx.gwteditors.client.impl1.edbean.EditableBean;
import org.sgx.gwteditors.client.util.TestUtil;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.resources.client.ExternalTextResource;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Panel;

/**
 * PH table 1
 * @author sg
 * 
 */
public class PropertyHaverTableEditor2Test1 implements Test {

	@Override
	public void test(Panel parent) {
		new EditorFramework1().start(); // do not forget of initialize the gwteditors framework !
		final PropertyHaverTableEditor2<Contact> ed = new PropertyHaverTableEditor2<Contact>();
		//override click behavior.
		ed.setClickHandler(new EditorClickHandler() {
			@Override
			public void call(Object val, int column, int index) {
				//nothing
			}
		}); 
		ed.load(getAllContacts());
		parent.add(ed); 
		parent.add(new Button("Get Selection", new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {
				String s = ""; 
				for(Contact c : ed.getSelectedModel()) {
					s+=c.getName()+", "; 
				}
				Window.alert("Selection: "+s);  
			}
		})); 
	}
	
	//model
	public static abstract class Contact implements EditableBean {
		String name, address;
		Boolean male; 
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getAddress() {
			return address;
		}
		public void setAddress(String address) {
			this.address = address;
		}
		public Boolean getMale() {
			return male;
		}
		public void setMale(Boolean male) {
			this.male = male;
		} 		
	}
	private static List<Contact> getAllContacts() {
		final TestUtil testUtil = TestUtil.getInstance(); 
		List<Contact> l = new LinkedList<PropertyHaverTableEditor2Test1.Contact>(); 
		for (int i = 0; i < 12; i++) {
			Contact c = GWT.create(Contact.class);
			c.setName(testUtil.randomLetters(testUtil.randomBetween(10,20))); 
			c.setAddress(testUtil.randomLetters(testUtil.randomBetween(10,20))); 
			//order properties
			c.getProperties().reorder(new String[]{"name", "address", "male"}); 
			l.add(c); 
		}
		return l; 
	}
	
	// test stuff
	@Override
	public String getName() {
		return "PH table 2";
	}

	@Override
	public Map<String, ExternalTextResource> getJavaSources() {
		return TestUtil.getResource(getName(), TestResources.INSTANCE.PropertyHaverTableEditor2Test1());		
	}

	@Override
	public String[] getTags() {
		return new String[] {};
	}

	@Override
	public String getDescription() {
		return "PH table 1";
	}
}
